package edu.neusoft.yuejian_09.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import edu.neusoft.yuejian_09.R;
import edu.neusoft.yuejian_09.base.Constant;
import edu.neusoft.yuejian_09.base.RetrofitListener;
import edu.neusoft.yuejian_09.bean.Activities;
import edu.neusoft.yuejian_09.bean.Collection;
import edu.neusoft.yuejian_09.model.ActivityModel;
import edu.neusoft.yuejian_09.model.CollectModel;
import it.sephiroth.android.library.picasso.Picasso;

import static edu.neusoft.yuejian_09.base.Constant.PHOTO_URL;

public class ActInfoActivity extends Activity implements RetrofitListener {

    private TextView a_name,address,date,price,man_count,phone,intro;
    private ImageView img_return,img_collect,pic;
    private Button enroll;
    private int user_id;
    private int activity_id;
    private String activity_name;
    private int activity_price;
    private String activity_pic;
    private String activity_intro;
    private String activity_address;
    private String activity_date;
    private int activity_phone;
    private int activity_hot;
    private Activities activities;
    private Collection collectResult;
    private String coresult="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_act_info);
        initViews();
        initEvents();
        initData();
    }

    public void initViews(){
        a_name=(TextView)findViewById(R.id.activity_name);
        address=(TextView)findViewById(R.id.address);
        date=(TextView)findViewById(R.id.date);
        price=(TextView)findViewById(R.id.price);
        man_count=(TextView)findViewById(R.id.man_count);
        phone=(TextView)findViewById(R.id.phone);
        intro=(TextView)findViewById(R.id.info);
        img_return=(ImageView) findViewById(R.id.img_return);
        img_collect=(ImageView) findViewById(R.id.collect);
        pic=(ImageView) findViewById(R.id.pic);
        enroll=(Button) findViewById(R.id.enroll);
    }

    public void initEvents(){

        img_return.setOnClickListener(new View.OnClickListener() {//返回
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        img_collect.setOnClickListener(new View.OnClickListener() {//收藏
            @Override
            public void onClick(View v) {
                collectActivity();
            }
        });

        enroll.setOnClickListener(new View.OnClickListener(){//购买跳转
            @Override
            public void onClick(View v){
                Intent intent = new Intent(ActInfoActivity.this, EnrollActActivity.class);
                Bundle bundle  = new Bundle();
                bundle.putString("activity_name",activity_name);
                bundle.putInt("activity_price",activity_price);
                bundle.putInt("activity_id",activity_id);
                bundle.putString("activity_intro",activity_intro);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }

    public void initData(){

        getActivityInfo();//获取活动详情
        getUserId();//获取用户ID
        checkCollect();//检查收藏状态
    }

    //获取活动信息数据
    public void getActivityInfo(){
        Bundle bundle = getIntent().getExtras();
        activity_id = bundle.getInt("activity_id");
        ActivityModel activityModel = new ActivityModel();
        activityModel.getActivityById(activity_id,this);
    }

    //获取个人用户信息
    public void getUserId(){
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getInt("user_id", 0);
    }

    //收藏活动
    private void collectActivity( ){
        CollectModel collectModel = new CollectModel();
        collectModel.collectActivity(user_id,activity_id,this);
    }

    //检查收藏状态
    private void checkCollect(){
        CollectModel collectModel = new CollectModel();
        collectModel.isCollected(user_id,activity_id,1,this);
    }

    private void refresh(){
        checkCollect();
    }

    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag){
            case Constant.ACTIVITYBYID:
                Activities activities=(Activities) o;
                activity_name = activities.getActivity_name();
                activity_price = activities.getActivity_price();
                activity_pic =PHOTO_URL+activities.getActivity_pic();
                activity_intro=activities.getActivity_intro();
                activity_address=activities.getActivity_address();
                activity_date=activities.getActivity_time();
                activity_phone=activities.getActivity_phone();
                activity_hot=activities.getActivity_hot();
                a_name.setText(activity_name);
                price.setText("￥" + activity_price);
                intro.setText(activity_intro);
                address.setText(activity_address);
                date.setText(activity_date);
                phone.setText(activity_phone+"");
                man_count.setText(activity_hot+"");
                if (activity_pic != "") {
                    Picasso.with(ActInfoActivity.this)
                            .load(activity_pic)
                            .into(pic);
                    pic.setScaleType(ImageView.ScaleType.FIT_XY);
                } else {
                    pic.setImageResource(R.drawable.no_pic);
                }
                break;
            case Constant.COLLECTACTIVITY:
                Collection collection=(Collection)o;

                    if ("yes".equals(coresult)){
                        Toast.makeText(ActInfoActivity.this,"取消收藏成功！",Toast.LENGTH_SHORT).show();
                        img_collect.setImageResource(R.drawable.ic_collect);
                    }else{
                        Toast.makeText(ActInfoActivity.this,"收藏成功！",Toast.LENGTH_SHORT).show();
                        img_collect.setImageResource(R.drawable.ic_collect1);
                    }
                    refresh();
                break;
            case Constant.COLLECTSTATE:
                Collection collectstate=(Collection)o;

                    String result = collectstate.getCollected();
                    System.out.printf(result);
                    if ("0".equals(result)){
                        img_collect.setImageResource(R.drawable.ic_collect);
                        coresult = "no";
                    }else {
                        coresult = "yes";
                        img_collect.setImageResource(R.drawable.ic_collect1);
                    }

                break;
        }


    }
    @Override
    public void onFail(){ Toast.makeText(ActInfoActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}
}
